import logging
import traceback
import pymysql
import requests

from core.config import Config


class Shop(object):
    def __init__(self):
        pass

    @classmethod
    def get_shop_dispatch_mode(cls):

        shop_dispatch_mode_dict = {}
        db = pymysql.connect(**Config.ofs_shop_center)
        try:
            cursor = db.cursor()
            sql = """
                -- 派单算法 2.0 仓 ID
                SELECT
                    shop_id, -- 仓 ID
                    value -- 派单模式
                FROM ofs_shop_center.shop_config_info
                WHERE name = "Delivery"
            """
            cursor.execute(sql)
            results = cursor.fetchall()
            # 解析数据
            for row in results:
                # 1:  1.0   4:  2.0
                if str(row["value"]) == "1" or str(row["value"]) == "4":
                    shop_dispatch_mode_dict[str(row["shop_id"])] = str(row["value"])

            return shop_dispatch_mode_dict
        except Exception as e:
            traceback.print_exc()
            logging.error(f"获取派单门店失败: {e}")
        finally:
            db.close()